

#include "comm.hpp"

int main()
{
    char buffer[1024];

    SetNonBlock(0);

    while (true)
    {
        printf("Enter# ");
        fflush(stdout);
        ssize_t n = ::read(0, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            buffer[n] = 0;
            printf("echo$ %s", buffer);
        }
        else if (n == 0)
        {
            // 文件结尾、管道关闭
            printf("Read done\n");
            break;
        }
        else
        {
            // 根据 errno 来判断是 真的出错 还是 非阻塞
            if (errno == EWOULDBLOCK)
            {
                std::cout << "数据未就绪" << std::endl;
                // do other thing ...
                sleep(1);
                continue;
            }
            else if (errno == EINTR)
            {
                std::cout << "被信号中断，继续读取" << std::endl;
                continue;
            }
            else
            {
                perror("read");
                break;
            }
        }
    }

    return 0;
}